//
//  ZPayViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/16.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZPayViewController.h"
#import "ZLabelTools.h"
#import "ZPayViewLabelCell.h"
#import "ZPayYHTableViewCell.h"

#import "ZLabelTools.h"
#import "ZPaySucceedViewController.h"
#import "ZPayLoseViewController.h"

#import "LZMyTicketViewController.h"
static NSInteger  const PayVCButoonTag = 250;
static NSString * const KZPayIdentifier = @"KZPayIdentifier";
@interface ZPayViewController ()<UITableViewDelegate ,UITableViewDataSource>{
    UIView * _whiteView;// 尾部视图白色部分
    UILabel * _payWayLabel;//支付方式
    UIImageView * _AliPayImageView;
    UIImageView * _WEChatImageView;
    UILabel * _AliPayLabel;
    UILabel * _WEChatLabel;
    UIButton * _AliPayBtn;
    UIButton * _WeChatBtn;
    UIButton * _GoPayBtn;
    NSInteger _selectIndex;
//    UIView * _whiteView;
}
@property (nonatomic,strong) UITableView * mainTableView;
@property (nonatomic,strong) UIView * tabFootView;
@property (nonatomic,strong) LZUserInfo * userInfo;
@property (nonatomic,strong) NSMutableArray * dataArray;
@end

@implementation ZPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initialzeDatas];
    [self initlizeInterface];
}

- (void)initialzeDatas {
    _selectIndex = 0;
    _dataArray = [@[@"",@""] mutableCopy];
}
- (void)setPid:(NSString *)pid {
    _pid = pid;
    [self payDataNetWorking];
}

- (void)initlizeInterface {
    
    self.navigationItem.title = @"支付";
    [self.view addSubview:self.mainTableView];
    
    
}
- (void)MakeFootSubView {
    _whiteView = [[UIView alloc] init];
    _whiteView.backgroundColor = [UIColor whiteColor];
    
    _payWayLabel = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_payWayLabel Textcolor:KHGaryColor backColor:[UIColor whiteColor] Fontsize:15 context:@"选择支付方式" isCenter:NO];
    
    _AliPayImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"allpay"]];
    
    _AliPayLabel = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_AliPayLabel Textcolor:[UIColor blackColor] backColor:[UIColor whiteColor] Fontsize:15 context:@"支付宝支付" isCenter:NO];
    
    _AliPayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_AliPayBtn setImage:[UIImage imageNamed:@"jubao_gou"] forState:UIControlStateNormal];
    [_AliPayBtn addTarget:self action:@selector(AliPayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _AliPayBtn.tag = PayVCButoonTag+1;
    
    _WEChatImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"weipay"]];
    
    _WEChatLabel = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_WEChatLabel Textcolor:[UIColor blackColor] backColor:[UIColor whiteColor] Fontsize:15 context:@"微信支付" isCenter:NO];
    
    _WeChatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_WeChatBtn setImage:[UIImage imageNamed:@"jubao_gou"] forState:UIControlStateNormal];
    _WeChatBtn.tag = PayVCButoonTag+2;
    [_WeChatBtn addTarget:self action:@selector(WePayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _GoPayBtn =[UIButton buttonWithType:0];
    _GoPayBtn.backgroundColor = KHGreenColor;
    [_GoPayBtn setTitle:@"去支付" forState:UIControlStateNormal];
    [_GoPayBtn setTintColor:[UIColor whiteColor]];
    [_GoPayBtn addTarget:self action:@selector(goPayBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)addFootALLSubView {
    [self.tabFootView addSubview:_whiteView];
    [self.tabFootView addSubview:_payWayLabel];
    [self.tabFootView addSubview:_AliPayImageView];
    [self.tabFootView addSubview:_AliPayLabel];
    [self.tabFootView addSubview:_AliPayBtn];
    [self.tabFootView addSubview:_WEChatImageView];
    [self.tabFootView addSubview:_WEChatLabel];
    [self.tabFootView addSubview:_WeChatBtn];
    [self.tabFootView addSubview:_GoPayBtn];
}
- (void)MakeSubMasnoy {
    [_whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.top.equalTo(self.tabFootView);
        make.height.offset(150 *ysIZE);
    }];
    [_payWayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tabFootView).offset(10*ysIZE);
        make.top.equalTo(self.tabFootView).offset(10*ysIZE);
        make.height.offset(15*ysIZE);
        make.width.offset(120*xsIZE);
    }];
    [_AliPayImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_payWayLabel);
        make.top.equalTo(_payWayLabel.mas_bottom).offset(22*ysIZE);
        make.width.height.offset(30*xsIZE);
    }];
    [_AliPayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_AliPayImageView.mas_right).offset(10*xsIZE);
        make.centerY.equalTo(_AliPayImageView);
        make.height.offset(15*ysIZE);
        make.width.offset(80*xsIZE);
    }];
    [_AliPayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.tabFootView).offset(-10*xsIZE);
        make.width.height.offset(18*xsIZE);
        make.centerY.equalTo(_AliPayImageView);
    }];
    
    [_WEChatImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_AliPayImageView.mas_bottom).offset(28*ysIZE);
        make.left.width.height.equalTo(_AliPayImageView);
    }];
    [_WEChatLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_WEChatImageView);
        make.left.width.height.equalTo(_AliPayLabel);
    }];
    [_WeChatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_WEChatImageView);
        make.width.height.centerX.equalTo(_AliPayBtn);
    }];
    [_GoPayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tabFootView);
        make.width.equalTo(self.tabFootView).multipliedBy(0.8);
        make.height.offset(45*ysIZE);
        make.top.equalTo(_whiteView.mas_bottom).offset(30*ysIZE);
    }];
    UIView * grayView = [[UIView alloc] init];
    grayView.backgroundColor = KHColorFromRGB(0xe0e0e0);
    [self.tabFootView addSubview:grayView];
    [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_AliPayImageView);
        make.right.equalTo(_AliPayBtn);
        make.height.offset(ysIZE);
        make.top.equalTo(_WEChatImageView).offset(-14*ysIZE);
    }];
    
}
//网络请求
- (void)payDataNetWorking {
    _userInfo = [LZUserInfo getUserInfo];
    NSDictionary * dic = @{@"uid":_userInfo.uid,@"pid":self.pid};
    [YJProgressHUD showProgress:@"加载中" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dic paramarsSite:@"Order/payView" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] intValue]== 1) {
            [YJProgressHUD showMessage:@"加载完成" inView:self.view];
            [self handlDataWith:dict[@"list"]];
        }else {
            [YJProgressHUD showMessage:@"加载失败" inView:self.view];
        }
    } failure:^(NSError *error) {
         [YJProgressHUD showMessage:@"网络请求失败" inView:self.view];
    }];
    
}
- (void)payNetWorking {

    NSDictionary * dict = @{@"order_num":self.dataArray[0],@"price":self.dataArray[1]};
    [YJProgressHUD showProgress:@"支付中" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Order/pay" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"支付成功" inView:self.view];
            [self jumpVCWithCode:YES];
        }else {
            [YJProgressHUD showMessage:@"支付失败" inView:self.view];
            [self jumpVCWithCode:NO];
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求失败" inView:self.view];
    }];
}

//跳转
- (void)jumpVCWithCode:(BOOL ) sue {
    if (sue) {
        ZPaySucceedViewController * zPaySVC = [[ZPaySucceedViewController alloc] init];
        zPaySVC.order_num =  _dataArray[0];
        zPaySVC.isHome = self.isHome;
        [self.navigationController pushViewController:zPaySVC animated:YES];
        
    }else {
        ZPayLoseViewController * zPaySVC = [[ZPayLoseViewController alloc] init];
        [self.navigationController pushViewController:zPaySVC animated:YES];
    }
}
- (void)handlDataWith:(NSDictionary *)backDict {
    _dataArray[0] = backDict[@"order_num"];
    _dataArray[1] = backDict[@"price"];
    [_mainTableView reloadData];
}
// 去支付按钮方法
- (void)goPayBtnAction {
//    ZPaySucceedViewController * zPaySVC = [[ZPaySucceedViewController alloc] init];
//    [self.navigationController pushViewController:zPaySVC animated:YES];
//    ZPayLoseViewController * zPaySVC = [[ZPayLoseViewController alloc] init];
//    [self.navigationController pushViewController:zPaySVC animated:YES];
    [self payNetWorking];
}
// 选择微信支付
- (void)WePayButtonAction:(UIButton *)sender {
    [_WeChatBtn setImage:[UIImage imageNamed:@"jubao_gou_pre"] forState:UIControlStateNormal];
    [_AliPayBtn setImage:[UIImage imageNamed:@"jubao_gou"] forState:UIControlStateNormal];
}
// 选择支付宝
- (void)AliPayButtonAction:(UIButton *)sender {
    [_WeChatBtn setImage:[UIImage imageNamed:@"jubao_gou"] forState:UIControlStateNormal];
    [_AliPayBtn setImage:[UIImage imageNamed:@"jubao_gou_pre"] forState:UIControlStateNormal];
}

// 表格视图协议
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }else if (section == 1) {
        return 1;
    }
    return 0;
}
//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10*ysIZE;//section头部高度
}

//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 0*ysIZE;
    }
    return 10*ysIZE;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:KZPayIdentifier];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"订单号:";
            cell.detailTextLabel.textColor = KHGaryColor;
            cell.detailTextLabel.text = _dataArray[0];
        }else {
        cell.detailTextLabel.textColor = KHColorFromRGB(0xe94444);
        cell.textLabel.text = @"待支付金额:";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@元",_dataArray[1]];
        }
       
    }else {
        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = @"优惠券";
        cell.detailTextLabel.text = @"                               点击选择";
        cell.detailTextLabel.textColor = KHGaryColor;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        NSLog(@"进入优惠券界面");
        LZMyTicketViewController * LMTVC = [[LZMyTicketViewController alloc] init];
        [self .navigationController pushViewController:LMTVC animated:YES];
    }
    return;
}



//lazy
- (UITableView *)mainTableView {
    if (_mainTableView) {
        return _mainTableView;
    }
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_Width, SCREEN_Height) style:UITableViewStylePlain];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [_mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:KZPayIdentifier];
    _mainTableView.rowHeight = 50*ysIZE;
    _mainTableView.tableFooterView = self.tabFootView;
    return _mainTableView;
}
- (UIView *)tabFootView {
    if (_tabFootView) {
        return _tabFootView;
    }
    _tabFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_Width, 450*ysIZE)];
    _tabFootView.backgroundColor = KHColorFromRGB(0xf8f8f8);
    [self MakeFootSubView];
    [self addFootALLSubView];
    [self MakeSubMasnoy];
    return _tabFootView;
}




@end
